﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Data.SqlClient;

namespace ResUma
{
    public partial class GrilaOcupatiiDepFrm : FrmRezumat
    {
        protected int _IDTipSpor = -1;
        public GrilaOcupatiiDepFrm(ModuleClass mc,int idtipspor):base(mc)
        {
            _IDTipSpor = idtipspor;
            InitializeComponent();
            workConnString = "ESRPConnectionString";
            ActiveControl = grileOcupatiiDGV;
        }

        
        private void GrilaOcupatiiFrm_Load(object sender, EventArgs e)
        {

            SqlConnection con = DeschideConexiune(workConnString);

            Object ret = GetSetupValue("ResUmaSetup", "VersiuneCOR", ModuleInfo.CodUnitate, con);
            Int16 versiuneCOR = Convert.ToInt16(ret);
            
            SqlCommand com = new SqlCommand("INSERT INTO [GrileOcupatiiDep] ([IDOcupatie],[IDDepartament],[Valoare],[IDTipSpor]) " +
                "SELECT IDOcupatie,IDDepartament,0," + _IDTipSpor.ToString() + " FROM [AngajatiSalarizare] a JOIN [Persoane] p  ON a.IdPersoana = p.IdPersoana " +
                "JOIN [AngajatiOcupatii] c on c.IDPersoana=p.IdPersoana  WHERE p.CodUnitate = " + ModuleInfo.CodUnitate.ToString() + " AND IDDepartament<>0 AND NOT EXISTS(SELECT * FROM [GrileOcupatiiDep] dd WHERE dd.IDDepartament= " +
                "a.IDDepartament and c.IDOcupatie=dd.IDOcupatie and [IDTipSpor]=" + _IDTipSpor.ToString() + ") GROUP BY IDDepartament,IDOcupatie", con);
            com.ExecuteNonQuery();

            this.ocupatiiTableAdapter.Connection = con;
            this.ocupatiiTableAdapter.FillByTerminalVersiune(this.eSRPDataSet.Ocupatii,versiuneCOR);

            departamenteTableAdapter.Connection = con;
            departamenteTableAdapter.FillByUtilizat(eSRPDataSet.Departamente, ModuleInfo.CodUnitate);
            this.grileOcupatiiDepTableAdapter.Connection = con;
            if (_IDTipSpor == -1)
                this.grileOcupatiiDepTableAdapter.Fill(this.eSRPDataSet.GrileOcupatiiDep);
            else
            {
                this.grileOcupatiiDepTableAdapter.FillByIDSpor(this.eSRPDataSet.GrileOcupatiiDep, _IDTipSpor);
                //IDTipSporColumn.Visible = false;
                eSRPDataSet.GrileOcupatii.IDTipSporColumn.DefaultValue = _IDTipSpor;
            }
            grileOcupatiiBindingSource.Sort = "IDDepartament,IDOcupatie";
            InchideConexiune(con);
            AddBindingSource(grileOcupatiiBindingSource);
            listTableAdaptor.Add(grileOcupatiiDepTableAdapter.Adapter);
            HandleSalvare += Salvare;
        }
    }
}
